package it.elis.media.library.controller;

import java.util.List;

import it.elis.media.library.model.data.Book;
import it.elis.media.library.service.BookService;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
//@RequestMapping(value="searchBook")
public class BookController {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	BookService bookService;

	
	/*Ciao ciao*/
	@RequestMapping(value = "/secured/open", method = RequestMethod.GET)
	public ModelAndView welcome(Model model) {
		logger.debug("opening");
		model.addAttribute("bookCriteria", new Book());
		return new ModelAndView("SearchBook");
	}

	@RequestMapping(value = "/secured/do", method = RequestMethod.GET)
	public ModelAndView search(@ModelAttribute("bookCriteria") Book bookCriteria, Model model) throws Exception {
		logger.info("searching " + bookCriteria.getTitle());
		List<Book> books = bookService.search(bookCriteria);
		model.addAttribute("list", books);
		return new ModelAndView("SearchBook");
	}

}
